package api

import (
	"admin100/app/model"
	"admin100/app/service"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"admin100/library/response"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

var SettingDataMgrApi = settingDataMgr{}

type settingDataMgr struct {
}

// @summary 后台设置数据列表-新增
// @tags 后台工具
// @produce json
// @param   name query string true "名称"
// @param   state query int true "开关状态"
// @router  /set-data/add [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *settingDataMgr) Add(r *ghttp.Request) {
	var (
		req         model.SettingDataApiAddReq
		serviceData model.SettingDataServiceAddReq
	)
	if err := r.ParseForm(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}

	if err := gconv.Struct(req, &serviceData); err != nil {
		defErr.Panic(define.ReqParamsIllegal)
	}

	u := service.Context.GetUser(r.Context())
	res := service.SettingDataService.Add(u, &serviceData)

	response.JsonSuc(r, res)
}

// @summary 后台设置数据列表-编辑
// @tags 后台工具
// @produce json
// @param   id query int true "要编辑的id"
// @param   name query string false "名称"
// @param   state query int false "开关状态"
// @router  /set-data/edit [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *settingDataMgr) Edit(r *ghttp.Request) {
	var (
		req         model.SettingDataApiEditReq
		serviceData model.SettingDataServiceEditReq
	)
	if err := r.ParseForm(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}

	if err := gconv.Struct(req, &serviceData); err != nil {
		defErr.Panic(define.ReqParamsIllegal)
	}

	u := service.Context.GetUser(r.Context())
	res := service.SettingDataService.Edit(u, &serviceData)

	response.JsonSuc(r, res)
}

// @summary 后台设置数据列表-删除
// @tags 后台工具
// @produce json
// @param   ids query int true "要编辑的id"
// @router  /set-data/del [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *settingDataMgr) Del(r *ghttp.Request) {
	var (
		req         model.SettingDatApiDelReq
		serviceData model.SettingDatServicesDelReq
	)
	if err := r.ParseForm(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}

	if err := gconv.Struct(req, &serviceData); err != nil {
		defErr.Panic(define.ReqParamsIllegal)
	}

	res := service.SettingDataService.Del(&serviceData)

	response.JsonSuc(r, res)
}

// @summary 后台设置数据列表
// @tags 后台工具
// @produce json
// @param   page query int true "当前页数"
// @param   pageSize query int true "每页数据数量"
// @router  /set-data/list [GET]
// @success 200 {object} response.JsonResponse "执行结果" {object} common.PageInfo "分页信息"
func (a *settingDataMgr) List(r *ghttp.Request) {
	var (
		req         model.SettingDataApiListReq
		serviceData model.SettingDataServiceListReq
	)
	if err := r.ParseForm(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}

	if err := gconv.Struct(req, &serviceData); err != nil {
		defErr.Panic(define.ReqParamsIllegal)
	}

	total, data := service.SettingDataService.List(&serviceData)

	response.JsonSuc(r, map[string]interface{}{
		"total": total,
		"rows":  data,
	})

}
